/*!
 * mars
 * Copyright(c) 2016 huangbinglong
 * MIT Licensed
 */

'use strict';

angular.module("dayu.ui.http",[])
    .provider("DayuHttp",function() {
        function DayuHttp(actionName) {
            this.actionName = actionName;
            this._baseUrl = "/service";
            this._actionKey = "y9action";
            this._params = {};
            this._params[this._actionKey] = {};
            this._params[this._actionKey].name = this.actionName;
            this._params[this._actionKey].params = {};
            this._params[this._actionKey].header = {};
        }

        DayuHttp.prototype.baseUrl = function(baseUrl) {
            this._baseUrl = baseUrl;
            return this;
        };

        DayuHttp.prototype.params = function(params) {
            this._params[this._actionKey].params =angular.merge({},this._params[this._actionKey].params,params||{});
            return this;
        };

        DayuHttp.prototype.header = function(header) {
            this._params[this._actionKey].header =angular.merge({},this._params[this._actionKey].header,header||{});
            return this;
        };

        DayuHttp.prototype.isLoading = function() {
          return !!this._loading;
        };

        this.$get = function($http) {
            DayuHttp.prototype.post = function(params,successCallback,errorCallback) {
                var self = this;
                var theParams = angular.copy(this._params);
                theParams[this._actionKey].params =
                    angular.merge({},theParams[this._actionKey].params,params||{});

                self._loading = true;
                return $http.post(this._baseUrl,theParams)
                    .then(function(res) {
                        self._loading = false;
                    if (res.status == 200 &&
                        ((res.data.zz_status && res.data.zz_status=='100') || (res.data.status && res.data.status=='100'))) {
                        if (successCallback) {
                            successCallback(res.data);
                        }
                        return res.data;
                    } else if (errorCallback){
                        errorCallback(res.data);
                    }
                    throw new Error(res.data.cause||"");
                },function(res) {
                    self._loading = false;
                    if (errorCallback) {
                        errorCallback(res.data);
                    }
                    return res.data;
                });
            };
            DayuHttp.prototype.get = function(params,successCallback,errorCallback) {
                var self =this;
                self._loading = true;
                return $http.get(this._baseUrl,
                    {params:angular.merge({},
                        this._params[this._actionKey].params,params||{})})
                    .then(function(res) {
                    self._loading = false;
                    if (res.status == 200 &&
                        ((res.data.zz_status && res.data.zz_status=='100') || (res.data.status && res.data.status=='100'))) {
                        if (successCallback) {
                            successCallback(res.data);
                        }
                        return res.data;
                    } else if (errorCallback){
                        errorCallback(res.data);
                    }
                    throw new Error(res.data.cause||"");
                },function(res) {
                    self._loading = false;
                    if (errorCallback) {
                        errorCallback(res.data);
                    }
                    return res.data;
                });
            };
            return function(actionName) {
                return new DayuHttp(actionName);
            };
        };
    });